#! /bin/sh

## This is the script that builds the worker instance 
## This should be run on the EC2 instance

## right now we're starting with AWS instance ami-1aad5273 
## eventually I want this to work with AWS or Rackspace. But starting with AWS

## fire up the base ami then do:
#   wget http://docloud.googlecode.com/hg/buildImage.sh
#   chmod +x buildImage.sh
#   sudo /home/ubuntu/buildImage.sh


#update/upgrade things
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade --yes

#get R in there
echo "deb http://cran.r-project.org/bin/linux/debian lenny-cran/" | sudo tee -a /etc/apt/sources.list
echo "deb-src http://cran.r-project.org/bin/linux/debian lenny-cran/" | sudo tee -a /etc/apt/sources.list
gpg --keyserver pgp.mit.edu --recv-key 381BA480
gpg -a --export 381BA480 > jranke_cran.asc
sudo apt-key add jranke_cran.asc
sudo apt-get update
sudo apt-get install --yes  r-base  r-base-dev r-cran-hmisc littler

# things I like
sudo apt-get install --yes htop ps-watcher


# install redis
sudo apt-get install redis-server

##  edit /etc/redis/redis.conf to comment out the bind line
sudo perl -pi -e 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis/redis.conf
sudo /etc/init.d/redis-server restart

## set up which CRAN repo to use
## if we change this, also change the path in install.r
echo "\nr <- getOption('repos') \nr['CRAN'] <- 'http://cran.us.r-project.org' \noptions(repos = r) \nrm(r) \n" > .Rprofile

## set up the library location
## this gets used in startWorkers.r and install.r as well
## so don't screw around with this casually 
mkdir /home/ubuntu/R
mkdir /home/ubuntu/R/library
echo "R_LIBS=/home/ubuntu/R/library \n" >.Renviron

## copy in the install.r script
wget http://docloud.googlecode.com/hg/install.r
chmod +x install.r 

## copy in the startWorkers.r script
wget http://docloud.googlecode.com/hg/startWorkers.r
chmod +x startWorkers.r 

## copy in the registerDoRedis.r script
wget http://docloud.googlecode.com/hg/registerDoRedis.r
chmod +x registerDoRedis.r 

## get Bryan Lewis' redis-worker-installer.sh script and run it
wget http://docloud.googlecode.com/hg/redis-worker-installer.sh
chmod +x redis-worker-installer.sh 
sudo /home/ubuntu/redis-worker-installer.sh

## install doRedis
sudo /home/ubuntu/install.r iterators
sudo /home/ubuntu/install.r codetools
sudo /home/ubuntu/install.r rredis
sudo /home/ubuntu/install.r doRedis


## replace /etc/rc.local with one of our own choosing
## rc.local will contain the logic to determin if the instance is a master or worker upon booting
cd /etc/
sudo rm rc.local
sudo wget http://docloud.googlecode.com/hg/rc.local
sudo chmod +x rc.local
cd /home/ubuntu/

## get the ps-watcher config file
wget http://docloud.googlecode.com/hg/ps-watcher.ini


## bundling up the AMI

#  start with some house cleaning to make sure everything is empty
sudo rm -f /home/ubuntu/.ssh/authorized_keys
sudo rm -f /root/.*hist* $HOME/.*hist*
sudo rm -f /var/log/*.gz


## AMI Built from the AWS GUI at this point




